﻿using System;
using System.Collections.Generic;
using System.IO;

//using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using SaleOfMoviesSystem.Xml.Helper.TestData;

namespace SaleOfMoviesSystem.Xml.Helper
{
    //https://www.cnblogs.com/cherenshuishou4451/archive/2019/10/29/11754039.html
    //Json.Net 官网： https://www.newtonsoft.com/json/help/html/QueryJson.htm
    public class JsonConfigHelper
    {
        public JObject jObject = null;

        public string this[string key]
        {
            get
            {
                string str = "";
                if (jObject != null)
                {
                    str = GetValue(key);
                }

                return str;
            }
        }

        public JsonConfigHelper(string path)
        {
            jObject = new JObject();
            using (System.IO.StreamReader file = System.IO.File.OpenText(path))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jObject = JObject.Load(reader);
                }
            }

            ;
        }

        public T GetValue<T>(string key) where T : class
        {
            //return JsonConvert.DeserializeObject<T>(jObject.SelectToken(key));
            return JsonConvert.DeserializeObject<T>(jObject.SelectToken(key).ToString());
        }

        public string GetValue(string key)
        {
            return Regex.Replace((jObject.SelectToken(key).ToString()), @"\s", "");
        }
    }

    public class JsonHelper
    {
        /// <summary>
        /// 读取JSON文件
        /// </summary>
        /// <param name="key">JSON文件中的key值</param>
        /// <returns>JSON文件中的value值</returns>
        public string ReadJson(string key, string jsonFilePath)
        {
            //string jsonfile = "D://testJson.json";//JSON文件路径

            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonFilePath))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    var value = o[key].ToString();
                    return value;
                }
            }
        }

        public TResoult JsonRead<TResoult>(string jsonFilePath)
        {
            //some biz code
            string jsonString = File.ReadAllText(jsonFilePath);
            TResoult result = JsonConvert.DeserializeObject<TResoult>(jsonString);
            return result;
        }

        //Efficient C#：为什么要把泛型作为返回值  https://developer.aliyun.com/article/605327
        //TResoult GetT<TResoult, T1>(T1 t)
        //{
        //    //some biz code
        //    return default(TResoult);
        //}
    }

    [TestFixture]
    public class JsonHelperTests
    {
        [Test]
        public void ReadJsonTest()
        {
            //Console.WriteLine(JsonHelper.ReadJson("version", @"TestData\Test.json"));
            JsonHelper jsonHelper = new JsonHelper();
            var result1 = jsonHelper.ReadJson("version", @"Helper\TestData\Test.json");
            Assert.AreEqual("1", result1);
            //var result2 = jsonHelper.ReadJson("course", @"Helper\TestData\Test.json");
            //Assert.AreEqual("1", result2);
            //var result3 = jsonHelper.ReadJson("course", @"Helper\TestData\Test.json");
            //Assert.AreEqual("1", result3);
        }

        [Test]
        public void JsonReadTest()
        {
            var jsonresult = new JsonHelper().JsonRead<Test>(@"Helper\TestData\Test.json");
            Assert.AreEqual("1", jsonresult.course);
        }
    }

    [TestFixture]
    public class JsonConfigHelperTests
    {
        [Test]
        public void case1()
        {
            JObject jObject;
            JsonConfigHelper helper = new JsonConfigHelper(@"Helper\TestData\Test.json");
            jObject = helper.jObject as JObject; //获取Jobject对象

            int i = jObject.Count; //当前对象的节点的数量
            string result = (string)jObject["version"]; //直接读取当前键值
            //MessageBox.Show(str5);
            Assert.AreEqual("1", result);

            //JObject myjobj666;
            //myjobj666 = myjobj["Colleague"]["财务部"] as JObject;//当前节点下的子节点作为jobject对象
            ////实际测试过程中会异常报错未实例化,这是因为配件文件的保存格式问题,UTF-8
            //string str3 = (string)myjobj666["account"];
            //MessageBox.Show(str3);

            //string str1 = helper["SOCLASS[0].Name"];//
            //MessageBox.Show(str1);

            //JToken jken = myjobj["SOCLASS"];//键对象["SO"]内存在多组数据，读取的格式

            //foreach (JObject myobject in jken)
            //{
            //    string strr = (string)myobject["Name"];//读取每组节点下的某个键值的数据
            //    MessageBox.Show(strr);
            //}
            //if (myjobj.ContainsKey("SOCLASS"))  //判断jobject对象中是否存在"SO"这个键对象
            //{
            //    MessageBox.Show("done");
            //}
        }

        [Test]
        public void case2()
        {
            JObject jObject;
            JsonConfigHelper helper = new JsonConfigHelper(@"Helper\TestData\Test.json");
            jObject = helper.jObject as JObject; //获取Jobject对象

            int i = jObject.Count; //当前对象的节点的数量
            //string result = (string)jObject["score"][0]; //直接读取当前键值
            ////MessageBox.Show(str5);
            //Assert.AreEqual("85", result);

            string result = (string)jObject["score"][0].ToString(); //直接读取当前键值
            Assert.AreEqual("85", result);
            //JArray resArray = (JArray)jObject["test"]["array"];
            //Assert.AreEqual(new JArray[] { }, resArray[..3]);
        }
    }
}